package com.example.demo.internettongxin.tcptongxin2;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Description T2client
 * @Author songchuanlong
 * @Date 2025-05-30
 */
public class T2client {
    public static void main(String[] args) throws Exception {
        //1、创建socket对象，并同时请求与服务端程序的连接
        Socket socket = new Socket("127.0.0.1", 8888);
        //2、从socket通信管道中得到一个字节输出流，用来发数据给服务器端程序
        OutputStream os = socket.getOutputStream();
        //3、包装成高级输出流
        DataOutputStream dos = new DataOutputStream(os);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你的内容：");
            String msg = scanner.nextLine();
            //4、开始写数据出去
            dos.writeUTF(msg);
            if("exit".equals(msg)){
                System.out.println("退出成功！");
                dos.close();
                socket.close();//释放连接资源
                break;
            }
            dos.flush();
        }
    }
}
